﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows;
using System.Windows.Input;
using TotalApplication.Core;

namespace TotalApplication.AngelSix
{
    public partial class WindowViewModel : ObservableObject
    {
        #region public properties

        [ObservableProperty]
        private int minimizedWidth = 800;

        [ObservableProperty]
        private int minimizedHeight = 500;

        [ObservableProperty]
        private int innerContentPadding = 0;

        public bool Borderless
        {
            get
            {
                return _window.WindowState == WindowState.Maximized;
            }
        }
        public int ResizeBorder { get { return Borderless ? 0 : 6; } }

        public Thickness ResizeBorderThickness
        {
            get
            {
                return new Thickness(ResizeBorder + OuterMarginSize);
            }
        }

        public int OuterMarginSize
        {
            get
            {
                return _window.WindowState == WindowState.Maximized? 0 : _OuterShadowWidth;
            }
            set
            {
                _OuterShadowWidth = value;
            }
        }

        public Thickness OuterMarginSizeThicness
        {
            get
            {
                return new Thickness(OuterMarginSize);
            }
        }

        public int WindowRadius
        {
            get
            {
                return _window.WindowState == WindowState.Maximized ? 0 : _WindowRadius;
            }
            set
            {
                _WindowRadius = value;
            }
        }

        public CornerRadius WindowCornerRadius
        {
            get
            {
                return new CornerRadius(WindowRadius);
            }
        }

        public double TitleHeight { get;set; } = 42;

        public GridLength TitleHeightGridLength
        {
            get
            {
                return new GridLength(TitleHeight);
            }
        }

        /// <summary>
        /// The visibility of the Dimmed Overlay
        /// </summary>
        [ObservableProperty]
        private bool _dimmedOverlayVisible = false;


        #endregion

        #region private properties

        private Window _window;

        private int _OuterShadowWidth = 10;

        private int _WindowRadius = 10;

        #endregion

        #region commands

        [RelayCommand]
        public void Close()
        {
            _window.Close();
        }

        [RelayCommand]
        public void Minimize()
        {
            _window.WindowState = WindowState.Minimized;
        }

        [RelayCommand]
        public void Maxmize()
        {
            _window.WindowState ^= WindowState.Maximized;
        }

        [RelayCommand]
        public void Menu()
        {
            // Position of the mouse relative to the window
            Point point1 = Mouse.GetPosition(_window);
            //Point point2 = _window.PointToScreen(point1);

            // Add the window location
            Point point2 = new Point(point1.X + _window.Left, point1.Y + _window.Top);

            SystemCommands.ShowSystemMenu(_window, point2) ;
        }

        [RelayCommand]
        public void DimmedOverlayVisibleChange()
        {
            DimmedOverlayVisible = !DimmedOverlayVisible;
        }

        #endregion

        public WindowViewModel(Window window) { 
            this._window = window;

            this._window.StateChanged += (s, e) =>
            {
                OnPropertyChanged(nameof(ResizeBorderThickness));
                OnPropertyChanged(nameof(OuterMarginSize));
                OnPropertyChanged(nameof(OuterMarginSizeThicness));
                OnPropertyChanged(nameof(WindowRadius));
                OnPropertyChanged(nameof(WindowCornerRadius));
            };

            _window.Deactivated += (s, e) =>
            {
                DimmedOverlayVisible = true;
            };

            _window.Activated += (s, e) =>
            {
                DimmedOverlayVisible = false;
            };

            //var resizer = new WindowResizer(_window);
        }
    }
}
